<?php
/**
 * Created by PhpStorm.
 * User: sofwu
 * Date: 2019/1/11
 * Time: 下午3:13
 */
namespace App\Http\Controllers\Api\V1;

use App\Model\Ad;
use App\Http\Transformer\AdTransformer;

class AdController extends Controller
{
    /**
     * 获取广告
     */
    public function adList()
    {
        $rules = [
            'position' => ['required', 'alpha'],
        ];

        $payload = app('request')->only('position');

        $validator = app('validator')->make($payload, $rules);

        if ($validator->fails()) {
            throw new \Dingo\Api\Exception\StoreResourceFailedException('Could not get ad list.', $validator->errors());
        }


        $adItem = Ad::where(['position'=> $payload['position'], 'enabled'=> 1])->orderBy('sort')->get();

        return $this->response->collection($adItem, new AdTransformer);
    }
}